#include "filletWidget.h"
#include "BRepFilletAPI_MakeFillet.hxx"
#include "TopExp.hxx"

filletWidget::filletWidget(modelSoft* myModelSoft, QWidget *parent)
	: myModelSoft(myModelSoft)
	,QWidget(parent)
	, ui(new Ui::filletWidgetClass())
{
	ui->setupUi(this);
	ui->modifyButton->setEnabled(false);
	ui->confirmButton->setEnabled(false);
	myModelSoft->myView->getInteractorStyle()->setFilletOperation(true);
	filletFeature = new FilletFeature();
	isModified = false;
	filletFeature->setFilletId(myModelSoft->shapeTag++);
	filletFeature->setRadius(0.0);
	//rho = 0.0;
	createConnection();
}

filletWidget::~filletWidget()
{
	delete ui;
}

void filletWidget::createConnection()
{
	connect(myModelSoft->currEdgeSelector, &OccEdgeSelector::sentFilletShape, this, &filletWidget::receiveFilletSelectedShape);
	connect(ui->modifyButton, &QPushButton::clicked, this, &filletWidget::onClickModify);
	connect(ui->confirmButton, &QPushButton::clicked, this, &filletWidget::onClickConfirm);
	connect(ui->cancelButton, &QPushButton::clicked, this, &filletWidget::close);
	connect(ui->edgeSelect, &QPushButton::clicked, this, &filletWidget::onClickEdgeSelect);
	connect(ui->cancelSelect, &QPushButton::clicked, this, &filletWidget::OnCancelSelEdge);
}

void filletWidget::onClickModify()
{
	filletFeature->setRadius(ui->radiusInput->text().toDouble());
	//判断圆角半径是否有效 以及是否被修改过
	if (!(IsNearlyZero(filletFeature->getRadius())) && !isModified) {
		BRepFilletAPI_MakeFillet MF(selectPipeline->getShape());
		MF.Add(filletFeature->getRadius(), TopoDS::Edge(selectSubEdge));

		resultPipeline = myModelSoft->CreatePipeline(MF.Shape(), filletFeature->getFilletId(), true);
		filletFeature->setResultId(filletFeature->getFilletId());
		filletFeature->setShapeId(filletFeature->getFilletId());
		selectPipeline->SetPipelineVisibility(false);

		myModelSoft->myView->getRenderWindow()->Render();
		isModified = true;
		ui->confirmButton->setEnabled(true);
		ui->targetSelSubWidget->setStyleSheet("background-color:rgb(255,255,255)");
		myModelSoft->myView->getInteractorStyle()->clearCurrSelector();
	}
}

void filletWidget::onClickConfirm()
{
	QStandardItem* item1 = new QStandardItem(QString("倒圆角-%1").arg(filletFeature->getFilletId()));
	int row = myModelSoft->ui->model->rowCount();
	item1->setEditable(false);
	myModelSoft->ui->model->setItem(row, 0, item1);
	QStandardItem* item2 = new QStandardItem(QString("隐藏"));
	item2->setCheckable(true);
	myModelSoft->ui->model->setItem(row, 1, item2);
	myModelSoft->RemovePipelineFromRenderer(selectPipeline);
	myModelSoft->m_Features.insert(std::pair<Standard_Integer, FilletFeature*>(filletFeature->getFilletId(), filletFeature));
	filletFeature->AddOperation(filletFeature);
	confirmCreated = true;
	close();
}

void filletWidget::closeEvent(QCloseEvent* event)
{
	// 清除已选择的Actor
	myModelSoft->myView->getInteractorStyle()->clearCurrSelector();
	myModelSoft->myView->getInteractorStyle()->setFilletOperation(false);
	// 没有点确定，删除创建的Actor
	if (!confirmCreated)
	{
		selectPipeline->SetPipelineVisibility(true);
		if (!resultPipeline.IsNull())
		{
			myModelSoft->RemovePipelineFromRenderer(resultPipeline);
			myModelSoft->myView->getRenderWindow()->Render();
		}
	}

	close();
}

void filletWidget::onClickEdgeSelect()
{
	myModelSoft->myView->getInteractorStyle()->setSelectionMode(SM_Edge);
	myModelSoft->changeSelector(SM_Edge);
	myModelSoft->myView->getInteractorStyle()->setCurrSelector(myModelSoft->currSelector);

}


void filletWidget::OnCancelSelEdge()
{
	// 已经修改不能取消选中
	if (!selectPipeline.IsNull() && !isModified)
	{
		myModelSoft->RemoveSelectedActor(selectPipeline);
		selectPipeline = nullptr;
		selectSubEdge = TopoDS_Shape();
		ui->targetSelSubWidget->setStyleSheet("background-color:rgb(255,255,255)");
		ui->modifyButton->setEnabled(false);
		targetReceive = false;
	}
}


void filletWidget::receiveFilletSelectedShape(const TopoDS_Shape& subShape, Handle(QIVtkSelectionPipeline) pipeline)
{
	if (!targetReceive) {
		selectSubEdge = subShape;
		//查找选择边在父对象的索引值
		//映射容器，用于存储 TopoDS_Shape 对象的索引映射
		TopTools_IndexedMapOfShape M;
		//遍历 S 中的所有边	将边的索引添加到 M 中
		TopExp::MapShapes(pipeline->getShape(), TopAbs_EDGE, M);
		int count = M.Extent();
		while (count >= 0) {
			if (M.FindKey(count).IsEqual(selectSubEdge)) {
				filletFeature->setSubEdgeIndex(count);
				break;
			}
			count--;
		}
		selectPipeline = pipeline;
		//获取选择pipeline的特征
		int pipelineId = pipeline->GetShapeID();
		auto iter = myModelSoft->m_Features.find(pipelineId);
		if (iter == myModelSoft->m_Features.end()) {
			return;
		}
		BaseFeature* feature = iter->second;
        filletFeature->setTargetFeature(feature);
		targetReceive = true;
		ui->modifyButton->setEnabled(true);
		ui->targetSelSubWidget->setStyleSheet("background-color:rgb(255,178,102)");
	}
	
}

